import dayjs from 'dayjs'
import { LogService } from './log.service'
import { LogininfoEntity, Raw, TableQuery } from '@/common'
import { Controller, Delete, Get, ParseArrayPipe, Query, Res } from '@nestjs/common'

@Controller('monitor/log')
export class LogController {
  constructor(private readonly logService: LogService) {}

  /** 查询操作日志列表 */
  @Get('logininfo/list')
  public findLogininfoList(@TableQuery() queryParams: TableQueryParams<LogininfoEntity>) {
    return this.logService.findLogininfoList(queryParams)
  }

  /** 清空登录日志 */
  @Delete('logininfor/clear')
  public clearLogininfo() {
    return this.logService.clearLogininfo()
  }

  /** 删除登录日志 */
  @Delete('logininfor/delete')
  public deleteLogininfo(@Query('ids', new ParseArrayPipe()) ids: string[]) {
    return this.logService.deleteLogininfo(ids)
  }

  /** 导出登录日志 */
  @Raw()
  @Get('logininfor/exportExcel')
  public async exportLogininfoExcel(@Res() response: ExpressResponse) {
    const buffer = await this.logService.exportLogininfoExcel()
    const fileName = encodeURIComponent(`登录日志${dayjs().format('YYYYMMDD')}.xlsx`)
    response.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    response.setHeader('Content-Disposition', `attachment; filename="${fileName}"`)
    response.setHeader('Content-Length', buffer.byteLength)
    response.send(buffer)
  }
}
